﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Contracts.CcrDispatching;

namespace XcoAppSpaces.Core.CcrDispatching
{
	/// <summary>
	/// Service that provides ccr dispatchers and dispatcher queues for ccr task dispatching.
	/// </summary>
	public class CcrDispatcherService : ICcrDispatcherService
	{
		private readonly bool usingExternalDispatcher;
		private Dispatcher appSpaceInternalDispatcher; //an own dispatcher is used for the appspace-internal tasks

		/// <summary>
		/// Creates a new CcrDispatcherService.
		/// </summary>
		public CcrDispatcherService()
			: this(0)
		{
		}

		/// <summary>
		/// Creates a new CcrDispatcherService.
		/// </summary>
		/// <param name="ccrThreadCount">The number of threads that should be used.</param>
		public CcrDispatcherService(int ccrThreadCount)
		{
			usingExternalDispatcher = false;
			this.Dispatcher = new Dispatcher(ccrThreadCount, "XcoAppSpace.DefaultDispatcher");
			Initialize();
		}

		/// <summary>
		/// Creates a new CcrDispatcherService using an external dispatcher.
		/// </summary>
		/// <param name="dispatcher">The dispatcher that should be used.</param>
		public CcrDispatcherService(Dispatcher dispatcher)
		{
			usingExternalDispatcher = true;
			this.Dispatcher = dispatcher;
			Initialize();
		}

		private void Initialize()
		{
			this.DispatcherQueue = new DispatcherQueue("XcoAppSpace.DefaultDispatcherQueue", this.Dispatcher);
#if PocketPC
			this.appSpaceInternalDispatcher = new Dispatcher(2, "XcoAppSpace.InternalTaskDispatcher"); //default on CF seems to be only 1 thread, so give the appspace at least 2 threads to allow parallel send/receive
#else
			this.appSpaceInternalDispatcher = new Dispatcher(0, "XcoAppSpace.InternalTaskDispatcher");
#endif
			this.InternalDispatcherQueue = new DispatcherQueue("XcoAppSpace.InternalTaskDispatcherQueue", this.appSpaceInternalDispatcher);
		}

		#region ICcrDispatcherService Members

		/// <summary>
		/// The Ccr Dispatcher.
		/// </summary>
		public Dispatcher Dispatcher { get; private set; }

		/// <summary>
		/// The Ccr DispatcherQueue used for Worker Tasks.
		/// </summary>
		public DispatcherQueue DispatcherQueue { get; private set; }

		/// <summary>
		/// The Ccr DispatcherQueue used for AppSpace-internal tasks.
		/// </summary>
		public DispatcherQueue InternalDispatcherQueue { get; private set; }

		#endregion

		#region IXcoStatefulService Members

		/// <summary>
		/// Starts the service.
		/// </summary>
		public void Start()
		{
			//nothing to do
		}

		/// <summary>
		/// The start priority of the service.
		/// </summary>
		public int StartPriority
		{
			get { return 1; } //must be started early so the dispatcherqueue is accessible to other starting services
		}

		/// <summary>
		/// Stops the service by disposing the used Dispatchers und DispatcherQueues. If an external dispatcher is used, it is not disposed.
		/// </summary>
		public void Stop()
		{
			this.DispatcherQueue.Dispose();
			if (!usingExternalDispatcher)
			{
				this.Dispatcher.Dispose();
			}
			this.InternalDispatcherQueue.Dispose();
			this.appSpaceInternalDispatcher.Dispose();
		}

		#endregion

		#region IXcoService Members

		/// <summary>
		/// Initializes the service (does nothing in case of the CcrDispatcherService).
		/// </summary>
		/// <param name="serviceRegistry"></param>
		public void Initialize(XcoAppSpaces.Contracts.Service.IXcoServiceRegistry serviceRegistry)
		{
			//nothing to do
		}

		#endregion
	}
}
